package com.dxg.msns.favor.controller;

import com.dxg.msns.common.util.Result;
import com.dxg.msns.favor.pojo.MusicFavor;
import com.dxg.msns.favor.service.MusicFavorService;
import com.dxg.msns.music.pojo.Music;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (MusicFavor)表控制层
 *
 * @author makejava
 * @since 2020-03-13 20:46:05
 */
@RestController
@RequestMapping("/api/favor")
public class MusicFavorController {
    /**
     * 服务对象
     */
    @Autowired
    private MusicFavorService musicFavorService;


    /**
     * 查询用户音乐的收藏数
     */
    @GetMapping("/queryCountById")
    public Result<Integer> queryCountByUid(@RequestParam("id") String id) {
        Integer counts = this.musicFavorService.queryCountByUid(id);
        if (counts == null){
            return Result.success("用户未收藏");
        }
        return Result.success(counts);
    }

    /**
     * 查询用户音乐的收藏数
     */
    @GetMapping("/queryCountByMusicId")
    public Result<Integer> queryCountByMusicId(@RequestParam("musicId") String musicId) {
        Integer counts = this.musicFavorService.queryCountByMusicId(musicId);
        return Result.success(counts);
    }


    /**
     * 根据用户id查询该用户收藏音乐的id
     */
    @GetMapping("/queryById")
    public Result<List<com.dxg.msns.music.pojo.Music>> queryByUid(@RequestParam("id") String uid) {
        List<Music> musicList = this.musicFavorService.queryByUid(uid);
        return Result.success(musicList);
    }

    /**
     * 根据用户id和歌曲id查询收藏状态
     */
    @GetMapping("/queryFavor")
    public Result<Integer> queryFavor(@RequestParam("userId") String userId, @RequestParam("musicId") String musicId) {
        // 查询收藏状态
        Integer favorStatus = musicFavorService.queryFavorStatus(userId, musicId);
        // 返回查询结果，使用 Result.success() 封装查询结果
        return Result.success(favorStatus);
    }


    /**
     * 删除收藏信息
     *
     * @return
     */
    @GetMapping("/cancelFavor")
    public Result<Void> deleteFavor(@RequestParam("collectorId")String collectorId,
                                             @RequestParam("musicId")String musicId) {
        MusicFavor musicFavor = new MusicFavor();
        musicFavor.setCollectorId(collectorId);
        musicFavor.setMusicId(musicId);
        musicFavor.setFavorStatus(0);
        this.musicFavorService.deleteFavor(musicFavor);
        return Result.success("取消收藏成功");
    }

    /**
     * 新增关注
     * @return
     */
    @GetMapping("/addFavor")
    public Result<Void> addFavor(@RequestParam("collectorId")String collectorId,
                                 @RequestParam("musicId")String musicId) {
        MusicFavor musicFavor = new MusicFavor();
        musicFavor.setCollectorId(collectorId);
        musicFavor.setMusicId(musicId);
        String msg = musicFavorService.addFavor(musicFavor);
        return Result.success(msg);
    }
}